package net.lab1024.sa.admin.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.http.HttpUtil;
import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

@Slf4j
@Component
public class ImageUtils {
    @Value("${gif2webp.command:gif2webp}")
    private String gif2webpComand;

    public String webpmux(String oldFile, String newfile) {
        try {
            // 例如执行一个ping命令
            String command = gif2webpComand.replaceAll("gif2webp", "webpmux") + " -get frame 1 " + oldFile + " -o " + newfile;
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(command);
            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info("解析webp图片:{}", line);
            }
            // 等待命令执行完成
            process.waitFor();
            // 获取退出值
            int exitValue = process.exitValue();
            return "Exit value: " + exitValue;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public File readGifFirst(File file) {
        try {
            if (file.getAbsolutePath().endsWith(".webp")) {
                File tempFile = FileUtil.createTempFile(".png", true);
                webpmux(file.getAbsolutePath(), tempFile.getAbsolutePath());
                if (!tempFile.exists()) {
                    return file;
                }
                FileUtil.del(file);
                return tempFile;
            }
            GifDecoder gd = new GifDecoder();
            for (int i = 0; i < gd.getFrameCount(); i++) {
                BufferedImage frame = gd.getFrame(i);
                File tempFile = FileUtil.createTempFile(".png", true);
                ImageIO.write(frame, "png", tempFile);
                FileUtil.del(file);
                return tempFile;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void resizeImageAll(File file, int maxWidth, int maxHeight) {
        try {
            if (file.exists()) {
                BufferedImage originalImage = ImageIO.read(file);
                BufferedImage scaledImage = new BufferedImage(maxWidth, maxHeight, originalImage.getType());
                Graphics g = scaledImage.createGraphics();
                g.drawImage(originalImage, 0, 0, maxWidth, maxHeight, null);
                g.dispose();
                ImageIO.write(scaledImage, "png", file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
